import javax.swing.*;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-09-19
 * Time: 8:45
 */
public class TestDemo {
    public static void main(String[] args) {
        MyStack stack = new MyStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        stack.push(6);
        System.out.println(stack.pop());//弹出栈顶元素，并删除
        System.out.println(stack.peek());//获取栈顶元素，不删除
        System.out.println(stack.peek());//获取栈顶元素，不删除
        System.out.println(stack.isEmpty());
    }
    //150.逆波兰表达式求值
    public int evalRPN(String[] tokens){
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            String val = tokens[i];
            if (!isOperation(val)){
                //如果不是运算符
                stack.push(Integer.parseInt(val));
            }else{
                //到底是啥运算符???
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(val){
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case  "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    private boolean isOperation(String x){
        if (x.equals("+") || x.equals("-")|| x.equals("*")|| x.equals("/")){
            return true;
        }
        return false;
    }

    public boolean IsPopOrder(int [] pushA, int [] popA) {
        Stack<Integer> stack = new Stack<>();
        int i = 0;
        int j = 0;
        while (i < pushA.length && j < popA.length){
            stack.push(pushA[i]);
            while (j < popA.length && !stack.empty() && stack.peek() == popA[j]){
                stack.pop();
                j++;
            }
            i++;
        }
        return stack.empty();
    }
}
